package com.raowb.ddns.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.raowb.ddns.config.AppConfig;
import com.raowb.ddns.task.CheckIPTask;
import com.raowb.ddns.util.AliYunUtil;
import com.raowb.ddns.util.IpUtil;
import com.raowb.test.domain.TestDemo;
import com.raowb.test.service.ITestDemoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测试demoController
 * 
 * @author raowb
 * @date 2022-01-18
 */
@RestController
public class DdnsController extends BaseController {

	@Autowired
	IpUtil ipUtil;
	@Autowired
	CheckIPTask checkIPTask;
	@Autowired
	AppConfig appConfig;
	@Autowired
	AliYunUtil aliYunUtil;

	/**
	 * 更新域名记录
	 */
	@GetMapping(value = "public/ddns/updateRecord")
	public AjaxResult updateRecord(
			@RequestParam(name = "domain",required = false) String domain,
			HttpServletRequest request) {

		updateIPforDomainRecord("raowenbiao.cn", "mytest",
				ipUtil.getIpAddress(request));
		return AjaxResult.success();
	}

	public boolean updateIPforDomainRecord(String configDomain, String rR,
			String ip) {
		String ipDomain = ipUtil.getIPByDomain(rR + "." + configDomain);

		if (!ip.equals(ipDomain)) {
			String recordId = aliYunUtil.queryATypeDomainRecordId(configDomain,
					rR);

			if ("".equals(recordId)) {
				aliYunUtil.addDomainRecord(configDomain, ip, rR, "A");
			} else {
				aliYunUtil.updateATypeDomainRecord(recordId, ip, rR);
			}
		}
		return true;
	}
}
